<?php

class Bet_Model_UserMapper extends Bet_Model_Mapper {

	public function __construct() {
		$this->setDbTableName("Bet_Model_DbTable_User");
		$this->setId('user_id');
	}

	/**
	 * Save or update an model object of a db instance
	 * @var Bet_Model_User $user
	 * @return boolean Saving result - TRUE if successed, otherwise FALSE
	 */
	public function save(Bet_Model_User $user) {
		$data = array(
			"user_id" => $user->getUserId(),
			"username" => $user->getUsername(),
			"password" => $user->getPassword(),
			"full_name" => $user->getFull_name(),
			"birthday" => $user->getBirthday(),
			"cid" => $user->getCid(),
			"gender" => $user->getGender(),
			"address" => $user->getAddress(),
			"city" => $user->getCity(),
			"nationality" => $user->getNationality(),
			"education" => $user->getEducation(),
			"phone" => $user->getPassword(),
			"cell_phone" => $user->getCell_phone(),
			"status" => $user->getStatus()
		);
		return parent::save($data);
	}

	public function getUserGroupsById($userId) {
		$roles = null;
		$table = $this->getDbTable();
		$row = $table->find($userId)->current();
		if ($row != null) {
			$roleRowset = $row->findManyToManyRowset("Bet_Model_DbTable_Group", "Bet_Model_DbTable_UserGroup");
			if (count($roleRowset) > 0) {
				$roles = array();
				foreach ($roleRowset as $roleRow) {
					$roles[] = $roleRow->name;
				}
			}
		}
		return $roles;
	}

}

